<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>marked</title>
</head>

<body>
    <script type="text/javascript" src="..\marked.min.js"></script>
    <script type="text/javascript" src="..\ww_marked.js"></script>
    <script>


        var ww = ww || {}
        ww.marked = ww.markMarked()



     
        ww.get = function (url, type, loaded, progress, error, abort) {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', url);
            xhr.responseType = (typeof (type) == "string") ? type : "arraybuffer"
            xhr.onload = function () {
                if (xhr.status < 400) {
                    if (typeof loaded == "function") {
                        loaded(xhr.response, xhr)
                    }
                }
            };
            xhr.onprogress = progress
            xhr.onerror = error
            xhr.onabort = abort
            xhr.send()
            return xhr
        }



        /*
                webFile.get = function (url, type, loaded, progress, error, abort) {
                    img.src = url
                    console.log(url)
                    img.onload = function () {
                        console.log(img)
                        // wwonload( iframe.contentDocument.body.innerText)
                    };
                    img.onprogress = progress
                    img.onerror = error
                    img.onabort = abort
                    document.body.appendChild(img);
                }
        
        
        
                webFile.get = function (url, type, loaded, progress, error, abort) {
                    console.log(url)
                    iframe.onload = function () {
                        console.log(iframe)
                        console.log(iframe.contentDocument.body.innerText)
                        wwonload(iframe.contentDocument.body.innerText)
                    };
                    iframe.src = url
                    iframe.onprogress = progress
                    iframe.onerror = error
                    iframe.onabort = abort
                }
        
        */


        ww.open = function (value) {
            console.log(value)
            input.value = value
            ww.baseUrl = value
            var hash = value.split("#")[1]

            ww.get(value, "text", 
            function (re) {
                var md = ww.marked(re)
                //console.log(md) 
                //iframe2.contentDocument.body.innerHTML = md
                div.innerHTML = md
                ww.Highlighter.clear() 
                if(hash){
                    window.location.hash = "#" + hash 
                }
            })
        }


        /* 
         context = document.createElement("canvas")
           context.getContext("2d").drawImage(img, 0, 0)
   
           context.toBlob(function (blob) {
               console.log(blob)
               // code...
           }, "image/jpeg", 1);
        */

        window.onload = function () {

            input = document.createElement("input")
            input.type = "text"
            input.value = "marked.md"
            document.body.appendChild(input);


            button = document.createElement("input")
            button.type = "button"
            button.value = "打开"
            button.onclick = function () {
                // alert(input.value)
                if (input.value) {
                    ww.open(input.value)
                }
            }
            document.body.appendChild(button);



            iframe = document.createElement("iframe")
            iframe.hidden = "hidden"
            document.body.appendChild(iframe);



            //iframe2 = document.createElement("iframe")
            //document.body.appendChild(iframe2);


            img = document.createElement("img")
            img.hidden = "hidden"
            document.body.appendChild(img);







            input2 = document.createElement("input")
            input2.type = "text"
            input2.value = "mark"
            document.body.appendChild(input2);


            button2 = document.createElement("input")
            button2.type = "button"
            button2.value = "搜索"
            button2.onclick = function () {

                console.log(ww.Highlighter.search(div, input2.value))
                ww.Highlighter.searchResult()

            }

            document.body.appendChild(button2);

 
            div = document.createElement("div")
            document.body.appendChild(div);

        }


    </script>

</body>

</html>